const Koa = require('koa');
const bodyparser = require('koa-bodyparser');
const static = require('koa-static');
const Router = require('koa-router');
const query = require('./db/index')

const app = new Koa();
const router = new Router();
const post = 3000;


app.use(static(__dirname + "/views"))
app.use(bodyparser());

router.post("/login",async ctx => {
    const {user,password} = ctx.request.body;
    // console.log(user,password);
    const data =await query(`select * from users where user = '${user}'`);
    if(data.length>0){
        console.log('登录成功');
    }else{
        await query(`insert into users(user,password) value ('${user}','${password}')`);
        console.log('添加并登录成功');
    }
    ctx.body = "login";
})

router.post("/zc",async ctx => {
    const {user,password} = ctx.request.body;
    
    await query(`insert into users(user,password) value ('${user}','${password}')`);
    console.log('添加成功');
    
    ctx.body = "注册成功";
})

app.use(router.routes());

app.listen(post,()=>{console.log(`服务器已启动：http://localhost:${post}`)});